#include "ResultSaver.h"
#include <vector>

using namespace std;

ResultSaver::ResultSaver()
{	
	string time;
	getCurrentTime(time);
	time.append(".txt");

	outFile.open(time.c_str(), ifstream::app);
}

void ResultSaver::saveShares(WMI_Result<WMI_Share>& shares)
{
	vector<WMI_Share*> vecShares = shares.getObjects();
	vector<WMI_Share*>::iterator iter;

	for (iter = vecShares.begin(); iter != vecShares.end(); iter++)
		saveShare(*(*iter));
}

void ResultSaver::saveProcess(WMI_Result<WMI_Process>& process)
{
	vector<WMI_Process*> vecProcess = process.getObjects();
	vector<WMI_Process*>::iterator iter;

	for (iter = vecProcess.begin(); iter != vecProcess.end(); iter++)
		saveProcess(*(*iter));
}



void ResultSaver::saveShare(WMI_Share& share)
{		
	outFile << "Name: " << string(share.getCaption().begin(), share.getCaption().end());
	outFile << "  Description: " << string(share.getDescription().begin(), share.getDescription().end());
	outFile << "  Path: " << string(share.getPath().begin(), share.getPath().end())<< endl;
}

void ResultSaver::saveProcess(WMI_Process& process)
{
	outFile << "Name: " << string(process.getCaption().begin(), process.getCaption().end());
	outFile << " Description: " << string(process.getDescription().begin(), process.getDescription().end()) ;
	outFile << " Command Line: " << string(process.getCommandLine().begin(), process.getCommandLine().end()) ;
	outFile << " Executable Path: " << string(process.getExecutablePath().begin(), process.getExecutablePath().end()) << endl;
}

void ResultSaver::saveData(ADCM_Computer& computer, CM_ResultCollection& res)
{
	outFile << "************ COMPUTER ****************" << endl;
	outFile << "Id: " << computer.getIdComputer() << endl;
	outFile << "Name: " << string(computer.getCn().begin(), computer.getCn().end()) << endl;
	outFile << "OS: " << string(computer.getOs().begin(), computer.getOs().end()) << endl;

	outFile << "----------  PROCESS ----------" << endl;
	saveProcess(res.getProcesses());

	outFile << "----------  SHARES  ----------" << endl;
	saveShares(res.getShares());

	outFile << "---------- REGISTRIES --------" << endl;
	saveRegistries(res.getRegistries());
}

void ResultSaver::getCurrentTime(string& time)
{
	SYSTEMTIME systemTime;	         
    GetSystemTime(&systemTime);
		

	char* aux = new char[10];
	sprintf(aux,"%d%d%d" ,systemTime.wDay,systemTime.wMonth,systemTime.wYear);	

	time.append(aux);
}

void ResultSaver::saveRegistries(WMI_Result<WMI_Registry>& registries)
{
	vector<WMI_Registry*> vecReg = registries.getObjects();
	vector<WMI_Registry*>::iterator iter;

	for (iter = vecReg.begin(); iter != vecReg.end(); iter++)
	{
		outFile << string((*iter)->getName().begin(), (*iter)->getName().end()) << endl;
	}
}